package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.CustomerTransfer;
import cn.wolfcode.rbac.mapper.CustomerMapper;
import cn.wolfcode.rbac.mapper.CustomerTransferMapper;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.ICustomerTransferService;
import cn.wolfcode.rbac.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerTransferServiceImpl implements ICustomerTransferService {

    @Autowired
    private CustomerTransferMapper customerTransferMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public void delete(Long id) {
        customerTransferMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(CustomerTransfer customerTransfer) {
        // 把客户的销售人员字段改为新的销售人员
        Long customerId = customerTransfer.getCustomer().getId();
        Long newSellerId = customerTransfer.getNewseller().getId();
        customerMapper.updateSeller(customerId,newSellerId);

        // 设置操作人
        customerTransfer.setOperator(UserContext.getCurrentUser());
        //  设置操作时间
       customerTransfer.setOperateTime(new Date());
        customerTransferMapper.insert(customerTransfer);
    }

    @Override
    public CustomerTransfer get(Long id) {
        return customerTransferMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CustomerTransfer> listAll() {
        return customerTransferMapper.selectAll();
    }

    @Override
    public void update(CustomerTransfer customerTransfer) {
        customerTransferMapper.updateByPrimaryKey(customerTransfer);
    }

    @Override
    public PageInfo query(QueryObject qo) {
        /*int count = customerTransferMapper.selectForCount(qo);
        // 判断是否有数据
        if(count == 0){
            return new PageResult(qo.getPageSize());
        }*/
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"input_time desc");  // 返回页码和每页数量

        // 查询结果集
        List<CustomerTransfer> list = customerTransferMapper.selectForList(qo);
//        return new PageResult(qo.getCurrentPage(),count,list,qo.getPageSize());
        return new PageInfo(list);
    }

}
